// StopWatch.cpp: Implementierung der Klasse CStopWatch.
//
//////////////////////////////////////////////////////////////////////

#include "StopWatch.h"


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Klasse CStopWatch
//////////////////////////////////////////////////////////////////////

CStopWatch::CStopWatch()
	{
	StartTime = 0;
	Running = false;
	}


CStopWatch::~CStopWatch()
	{
	}


void CStopWatch::Reset()
	{
	Running = false;
	StartTime = 0;
	}


void CStopWatch::Start()
	{
	StartTime = clock();
	Running = true;
	}


void CStopWatch::Stop()
	{
	if (!Running) return;
	StartTime = clock() - StartTime;
	Running = false;
	}


void CStopWatch::Restart()
	{
	if (Running) return;
	StartTime = clock() - StartTime;
	Running = true;
	}


void CStopWatch::Set(double AStartFrom)
	{
	if (Running)
		StartTime = clock() - clock_t(AStartFrom * double(CLOCKS_PER_SEC));
	else
		StartTime = clock_t(AStartFrom * double(CLOCKS_PER_SEC));
	}


CStopWatch::operator double() const
	{
	if (Running)
		return double(unsigned long(clock() - StartTime)) / double(CLOCKS_PER_SEC);
	else
		return double(unsigned long(StartTime)) / double(CLOCKS_PER_SEC);
	}


//////////////////////////////////////////////////////////////////////
// Klasse CCountdown
//////////////////////////////////////////////////////////////////////

CCountdown::CCountdown(double AStartFrom)
	{
	Running = false;
	StartFrom = AStartFrom;
	}


CCountdown::CCountdown()
	{
	Running = false;
	StartFrom = 0.0;
	}


CCountdown::~CCountdown()
	{
	}


void CCountdown::SetStartTime(double AStartFrom)
	// in sekunden; auf StartFrom, KEIN Start
	{
	StartFrom = AStartFrom;
	}


void CCountdown::Reset()
	{
	Timer.Stop();
	Timer.Reset();
	Running = false;
	}


void CCountdown::Start()
	{
	Timer.Start();
	Running = true;
	}


void CCountdown::Start(double AStartFrom)
// setzt auch neue Standard-Laufzeit
	{
	Timer.Start();
	Running = true;
	StartFrom = AStartFrom;
	}


void CCountdown::Stop()
	{
	if (Running)
		{
		Timer.Stop();
		Running = false;
		}
	}


void CCountdown::Restart()
	{
	if (!Running)
		{
		Timer.Restart();
		Running = true;
		}
	}


bool CCountdown::IsRunning() 
	{
	if (!Running)
		return false;
	else
		return !*this;
	}


bool CCountdown::IsRunning() const
	{
	return Running && Timer < StartFrom;
	}


CCountdown::operator bool() // true, wenn abgelaufen
	{
	if (Timer >= StartFrom && StartFrom > 0.0)
		{
		if (Running)
			{
			Running = false;
			Timer.Stop();
			}
		return true;
		}
	else 
		return false;
	}


double CCountdown::TimeToEnd()
	{
	if (Timer >= StartFrom)
		return 0;
	else
		if (Running)
			return StartFrom - Timer;
		else
			return StartFrom;
	}


double CCountdown::TimeToEnd() const
	{
	if (Timer >= StartFrom)
		return 0;
	else
		if (Running)
			return StartFrom - Timer;
		else
			return StartFrom;
	}
